package com.manor.dynamic.activity;


import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.tools.SdkVersionUtils;
import com.manor.common.CommonAppConfig;
import com.manor.common.Constants;
import com.manor.common.activity.AbsActivity;
import com.manor.common.event.VideoRecordEvent;
import com.manor.common.glide.ImgLoader;
import com.manor.common.http.HttpCallback;
import com.manor.common.interfaces.ImageResultCallback;
import com.manor.common.interfaces.VideoResultCallback;
import com.manor.common.utils.ClickUtil;
import com.manor.common.utils.DialogUitl;
import com.manor.common.utils.FilePathUtil;
import com.manor.common.utils.KLog;
import com.manor.common.utils.L;
import com.manor.common.utils.PictureSelectorUtils;
import com.manor.common.utils.ProcessImageUtil;
import com.manor.common.utils.RouteUtil;
import com.manor.common.utils.StringUtil;
import com.manor.common.utils.ToastUtil;
import com.manor.common.utils.WordUtil;
import com.manor.dynamic.R;
import com.manor.dynamic.adapter.PublishImgAdapter;
import com.manor.dynamic.custorm.FullyGridLayoutManager;
import com.manor.dynamic.custorm.VoicePlayView;
import com.manor.dynamic.dialog.DynamicLookImgDialogFragment;
import com.manor.dynamic.dialog.VideoDialogFragment;
import com.manor.dynamic.dialog.VoiceRecordDialogFragment;
import com.manor.dynamic.event.ImgEvent;
import com.manor.dynamic.event.SubmitDynamiFinish;
import com.manor.dynamic.http.DynamicHttpConsts;
import com.manor.dynamic.http.DynamicHttpUtil;
import com.manor.dynamic.upload.UploadBean;
import com.manor.dynamic.upload.UploadCallback;
import com.manor.dynamic.upload.UploadQnImpl;
import com.manor.dynamic.upload.UploadStrategy;
import com.manor.im.utils.VoiceMediaPlayerUtil;
import com.manor.video.activity.LocalVideoChooseActivity;
import com.manor.video.event.ChooseVideoEvent;
import com.orhanobut.logger.Logger;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;
import top.zibin.luban.OnRenameListener;

/**
 * Created by debug on 2019/7/22.
 * 发布动态
 */
@Route(path = RouteUtil.RELEASA_NEWS)
public class DynamicPublishActivity extends AbsActivity implements View.OnClickListener {
    private static final int CHOOSE_IMG_REQUEST = 1001;
    private static final int CHOOSE_CARMERA_REQUEST = 1002;
    private TextView mBtnPublish;
    private EditText mEtContent;
    private TextView mNum;

    private TextView mTvLocation;
    private Switch mCbLocation;

    private View mLlUploadType;

    private View mLlShowImg;
    private ImageView mIvLastImg;
    private TextView mTvImgNum;
    private View mBtnAddImg;

    private View mRlShowVideo;
    private ImageView mIvVideoImg;
    private RecyclerView rvPublishImg;

    private View mRlShowVoice;
    private VoicePlayView mVoicePlayView;

    private String mLocation;
    private ProcessImageUtil mImageUtil;
    private Dialog mChooseImageDialog;

    private String mVideoPath;

    private VoiceMediaPlayerUtil mVoiceMediaPlayerUtil;
    private File mVoiceFile;
    private int mVoiceSumTime;

    private UploadStrategy mUploadStrategy;
    private int mPublishType;
    private List<UploadBean> mUploadList;
    private String mContent;//文字内容

    private Dialog mLoading;
    private Dialog mChooseVideoDialog;
    List<LocalMedia> selectImgList = new ArrayList<>();
    PublishImgAdapter publishImgAdapter;
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };


    public static void forward(Context context) {
        context.startActivity(new Intent(context, DynamicPublishActivity.class));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dynamic_publish;
    }

    @Override
    protected void main() {
        super.main();

        mEtContent = findViewById(R.id.et_content);
        mNum = (TextView) findViewById(com.manor.video.R.id.num);
        mTvLocation = findViewById(R.id.location);
        mCbLocation = findViewById(R.id.cb_location);
        mLlUploadType = findViewById(R.id.btn_upload_type);
        mLlShowImg = findViewById(R.id.ll_show_img);
        mIvLastImg = findViewById(R.id.last_img);
        mBtnAddImg = findViewById(R.id.btn_addimg);
        mBtnPublish = findViewById(R.id.btn_publish);
        mRlShowVideo = findViewById(R.id.rl_video_show);
        mIvVideoImg = findViewById(R.id.video_thumb);
        mRlShowVoice = findViewById(R.id.ll_show_voice);
        mVoicePlayView = findViewById(R.id.voice_view);
        mTvImgNum = findViewById(R.id.tv_img_num);
        rvPublishImg = findViewById(R.id.rv_publish_img);
        findViewById(R.id.btn_img).setOnClickListener(this);
        findViewById(R.id.btn_video).setOnClickListener(this);
        findViewById(R.id.btn_voice).setOnClickListener(this);
        findViewById(R.id.btn_del_img).setOnClickListener(this);
        findViewById(R.id.btn_voice_del).setOnClickListener(this);
        findViewById(R.id.btn_del_video).setOnClickListener(this);
        mBtnAddImg.setOnClickListener(this);
        mIvLastImg.setOnClickListener(this);
        mBtnPublish.setOnClickListener(this);
        mRlShowVideo.setOnClickListener(this);
        mBtnPublish.setEnabled(false);
        int hasWriteContactsPermission = ContextCompat.checkSelfPermission(DynamicPublishActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE);//android.manifest.xml
        if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(DynamicPublishActivity.this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                ActivityCompat.requestPermissions(DynamicPublishActivity.this,
                        PERMISSIONS_STORAGE,
                        REQUEST_EXTERNAL_STORAGE);
            }
            ActivityCompat.requestPermissions(DynamicPublishActivity.this,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);
        }
        mCbLocation.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mTvLocation.setText(mLocation);
                } else {
                    mTvLocation.setText(WordUtil.getString(R.string.at_mars));
                }
            }
        });
        mEtContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    setPublishEnable(true);
                } else {
                    setPublishEnable(false);
                }
                if (mNum != null) {
                    mNum.setText(s.length() + "/200");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mImageUtil = new ProcessImageUtil(this);
        mImageUtil.setImageResultCallback(new ImageResultCallback() {
            @Override
            public void beforeCamera() {
            }

            @Override
            public void onSuccess(File file) {
                if (file == null) {
                    return;
                }
                if (mLlUploadType != null) {
                    mLlUploadType.setVisibility(View.GONE);
                }
                if (mLlShowImg != null) {
                    mLlShowImg.setVisibility(View.VISIBLE);
                    rvPublishImg.setVisibility(View.VISIBLE);
                }
                mBtnPublish.setEnabled(true);
                mBtnPublish.setBackgroundResource(R.drawable.bg_dynamic_publishings);
                mPublishType = Constants.DYNAMIC_TYPE_IMG;
//                ImgLoader.display(mContext, file, mIvLastImg);
                UploadBean uploadBean = new UploadBean();
                uploadBean.setOriginFile(file);
                uploadBean.setType(UploadBean.IMG);
                mUploadList.add(uploadBean);


                setImgSize();
             /*   if (mUploadList.size() == Constants.DYNAMIC_IMG_MAX_NUM) {
                    if (mBtnAddImg != null) {
                        mBtnAddImg.setVisibility(View.GONE);
                    }
                }*/
              /*  if (mTvImgNum != null) {
                    mTvImgNum.setText(String.format(WordUtil.getString(R.string.img_num_tip), mUploadList.size()));
                }*/
            }

            @Override
            public void onFailure() {
            }
        });
        mImageUtil.setVideosultCallback(new VideoResultCallback() {
            @Override
            public void onSuccess(final File path) {
                mPublishType = Constants.DYNAMIC_TYPE_VIDEO;
                mVideoPath = path.getAbsolutePath();
                UploadBean video = new UploadBean();
                video.setOriginFile(path);
                video.setType(UploadBean.VIDEO);
                if (mUploadList != null) {
                    mUploadList.clear();
                }
                mUploadList.add(video);
                createVideoThumb();
            }

            @Override
            public void onFailure() {
            }
        });
        mUploadList = new ArrayList<>();
        mLocation = CommonAppConfig.getInstance().getCity();
        mTvLocation.setText(mLocation);
        EventBus.getDefault().register(this);
        CommonAppConfig.getInstance().setVideoPublishType(Constants.DYNAMIC_VIDEO_PUBLISH);
    }

    private void setPublishEnable(boolean isEnable) {
        if (TextUtils.isEmpty(mContent) || mUploadList.size() > 0) {
            mBtnPublish.setEnabled(isEnable);
            if(isEnable){
                mBtnPublish.setBackgroundResource(R.drawable.bg_dynamic_publishings);
            }
        } else {
            mBtnPublish.setEnabled(true);
            if(isEnable){
                mBtnPublish.setBackgroundResource(R.drawable.bg_dynamic_publishings);
            }
        }
    }

    /**
     * 选择图片
     */
    private void chooseImage() {
        if (mChooseImageDialog == null) {
            mChooseImageDialog = DialogUitl.getStringArrayDialog(mContext, new Integer[]{
                    R.string.take_photo, R.string.from_album}, true, new DialogUitl.StringArrayDialogCallback() {
                @Override
                public void onItemClick(String text, int tag) {
                    if (tag == R.string.take_photo) {
//                        mImageUtil.getImageByCamera(false);
                        selectCamera();
                    } else {
//                        ChooseImageActivity.forward(mContext, mUploadList.size());
                        selectLocalImg();
                    }
                }
            });
        }
        mChooseImageDialog.show();
    }
    PictureSelectorUtils pictureSelectorUtils = new PictureSelectorUtils();
    /**
     * 选择拍照
     */
    private void selectCamera() {


         pictureSelectorUtils.selectCamera(DynamicPublishActivity.this,selectImgList, new PictureSelectorUtils.IAddImageListener() {
            @Override
            public void onSelectedPictures(List<LocalMedia> list) {
                if (selectImgList.size() > 0) {
                    selectImgList.clear();
                }
                if (mUploadList.size() > 0) {
                    mUploadList.clear();
                }
                selectImgList = list;
                if (selectImgList.size() > 0) {
                    if (mLlUploadType != null) {
                        mLlUploadType.setVisibility(View.GONE);
                        rvPublishImg.setVisibility(View.VISIBLE);
                    }
                    if (mLlShowImg != null) {
                        mLlShowImg.setVisibility(View.VISIBLE);
                    }
                    mBtnPublish.setEnabled(true);
                    mBtnPublish.setBackgroundResource(R.drawable.bg_dynamic_publishings);
                    mPublishType = Constants.DYNAMIC_TYPE_IMG;

                    rvPublishImg.setVisibility(View.VISIBLE);

                    for (int i = 0; i < selectImgList.size(); i++) {
                        UploadBean uploadBean = new UploadBean();
                        if (SdkVersionUtils.checkedAndroid_Q()) {
                            uploadBean.setOriginFile(new File(String.valueOf(selectImgList.get(i).getAndroidQToPath())));
                        }else {
                            uploadBean.setOriginFile(new File(String.valueOf(selectImgList.get(i).getPath())));
                        }
                        uploadBean.setType(UploadBean.IMG);
                        mUploadList.add(uploadBean);
                    }
                    //TODO 修改九宫格照片
//                        mUploadList.addAll(mUploadList.size(), imgEvent.getImgBeanList());

                    setImgSize();
                }
            }
        });
    }

    /**
     * 选择本地相册
     */
    private void selectLocalImg() {
      pictureSelectorUtils.selectLocalImg(DynamicPublishActivity.this,selectImgList,9,1, new PictureSelectorUtils.IAddImageListener() {
            @Override
            public void onSelectedPictures(List<LocalMedia> list) {
                if (selectImgList.size() > 0) {
                    selectImgList.clear();
                }
                if (mUploadList.size() > 0) {
                    mUploadList.clear();
                }
                selectImgList = list;
                if (selectImgList.size() > 0) {
                    if (mLlUploadType != null) {
                        mLlUploadType.setVisibility(View.GONE);
                        rvPublishImg.setVisibility(View.VISIBLE);
                    }
                    if (mLlShowImg != null) {
                        mLlShowImg.setVisibility(View.VISIBLE);
                    }
                    mBtnPublish.setEnabled(true);
                    mBtnPublish.setBackgroundResource(R.drawable.bg_dynamic_publishings);
                    mPublishType = Constants.DYNAMIC_TYPE_IMG;

                    rvPublishImg.setVisibility(View.VISIBLE);

                    for (int i = 0; i < selectImgList.size(); i++) {
                        UploadBean uploadBean = new UploadBean();
                        if (SdkVersionUtils.checkedAndroid_Q()) {
                            uploadBean.setOriginFile(new File(String.valueOf(selectImgList.get(i).getAndroidQToPath())));
                        }else {
                            uploadBean.setOriginFile(new File(String.valueOf(selectImgList.get(i).getPath())));
                        }
                        uploadBean.setType(UploadBean.IMG);
                        mUploadList.add(uploadBean);
                    }
                    //TODO 修改九宫格照片
//                        mUploadList.addAll(mUploadList.size(), imgEvent.getImgBeanList());

                    setImgSize();
                }
            }
        });


    }

    /**
     * 选择上传视频
     */
    public void chooseVideo() {
        if (mChooseVideoDialog == null) {
            mChooseVideoDialog = DialogUitl.getStringArrayDialog(mContext, new Integer[]{
                    R.string.video_record, R.string.video_local}, true, new DialogUitl.StringArrayDialogCallback() {
                @Override
                public void onItemClick(String text, int tag) {
                    if (tag == R.string.video_record) {
                        if (mImageUtil != null) {
                            mImageUtil.getVideoRecord();
                        }
                    } else {
                        selectLocalVideo();
//                        chooseVides();
                    }
                }
            });
        }
        mChooseVideoDialog.show();

    }


    /**
     * 点击上传，选择本地视频
     */
    private void selectLocalVideo() {
        Intent intent = new Intent(mContext, LocalVideoChooseActivity.class);
        startActivity(intent);
    }


    private void recordVoice() {//

        AndPermission.with(this)
                .runtime()
                .permission(Permission.RECORD_AUDIO)
                .onGranted(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        if (!lacksPermission(DynamicPublishActivity.this, Permission.ACCESS_COARSE_LOCATION)
                        ) {
                            VoiceRecordDialogFragment voiceRecordDialogFragment = new VoiceRecordDialogFragment();
                            voiceRecordDialogFragment.show(getSupportFragmentManager(), "VoiceRecordDialogFragment");
                        }
                    }
                })
                .onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        for (int i = 0; i < data.size(); i++) {
                            KLog.e("tag", data.get(i) + "");
                            if (lacksPermission(DynamicPublishActivity.this, Permission.RECORD_AUDIO)) {
                                Toast.makeText(DynamicPublishActivity.this,
                                        getResources().getString(R.string.permissions_audio), Toast.LENGTH_SHORT).show();
                                if (AndPermission.hasAlwaysDeniedPermission(DynamicPublishActivity.this, data)) {
                                    showSettingDialog(DynamicPublishActivity.this,
                                            getResources().getString(R.string.permissions_audio),
                                            0);
                                } else {

                                }
                                return;
                            }

                        }

                    }
                })
                .start();


    }


    /**
     * 查看选择的所有图片
     */
    private void lookAllImgs(int selectPos) {
        DynamicLookImgDialogFragment lookImgDialogFragment = new DynamicLookImgDialogFragment();
        Bundle bundle = new Bundle();
        ArrayList<String> list = new ArrayList<>();
        for (UploadBean bean : mUploadList) {
            if(null!=bean.getOriginFile()&&!bean.getOriginFile().equals("")){
                list.add(bean.getOriginFile().getAbsolutePath());
            }

        }
        bundle.putString(Constants.DYNAMIC_UID, CommonAppConfig.getInstance().getUid());
        bundle.putStringArrayList(Constants.DYNAMIC_IMG_LIST, list);
        bundle.putInt(Constants.DYNAMIC_IMG_CUR_POS, selectPos);
        lookImgDialogFragment.setArguments(bundle);
        lookImgDialogFragment.show(getSupportFragmentManager(), "DynamicLookImgDialogFragment");
    }

/*    //查看图片
    public void lookImgs(ArrayList<String> list, int selectPos) {
        DynamicLookImgDialogFragment dynamicImgsDialogFragment = new DynamicLookImgDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putStringArrayList(Constants.DYNAMIC_IMG_LIST, list);
        bundle.putInt(Constants.DYNAMIC_IMG_CUR_POS, selectPos);
        bundle.putString(Constants.DYNAMIC_UID, "");
        dynamicImgsDialogFragment.setArguments(bundle);
        dynamicImgsDialogFragment.show(getSupportFragmentManager(), "LookDynamicImgsDialogFragment");
    }
    */


    /**
     * 观看视频
     */
    private void lookVideo() {
        VideoDialogFragment videoDialogFragment = new VideoDialogFragment();
        videoDialogFragment.show(getSupportFragmentManager(), "VideoDialogFragment");
    }


    public void delUploadImgs(int pos) {
        boolean isDelLast = false;
        if (pos == mUploadList.size() - 1) {
            isDelLast = true;
        }
        mUploadList.remove(pos);
        if (mUploadList.size() == 0) {
            mPublishType = Constants.DYNAMIC_TYPE_TEXT;
            if (mLlShowImg != null) {
                mLlShowImg.setVisibility(View.GONE);
            }
            if (mLlUploadType != null) {
                mLlUploadType.setVisibility(View.VISIBLE);
            }
            setPublishEnable(false);
        } else {
            if (isDelLast) {
                int lastSize = mUploadList.size() - 1;
                ImgLoader.display(mContext, mUploadList.get(lastSize).getOriginFile(), mIvLastImg);
            }
        }
        if (mUploadList.size() < 1) {
            rvPublishImg.setVisibility(View.GONE);
            mLlUploadType.setVisibility(View.VISIBLE);
        } else {
            mLlUploadType.setVisibility(View.GONE);
        }
        publishImgAdapter.notifyDataSetChanged();
    }

    /**
     * 删除所有图片
     */
    private void delAllImgs() {
        DialogUitl.showSimpleDialog3(mContext, WordUtil.getString(R.string.dynamic_del_all_tip), WordUtil.getString(R.string.delete), "", new DialogUitl.SimpleCallback() {
            @Override
            public void onConfirmClick(Dialog dialog, String content) {
                mPublishType = Constants.DYNAMIC_TYPE_TEXT;
                if (mUploadList != null) {
                    mUploadList.clear();
                }
                if (mLlShowImg != null) {
                    mLlShowImg.setVisibility(View.GONE);
                }
                if (mLlUploadType != null) {
                    mLlUploadType.setVisibility(View.VISIBLE);
                }
                setPublishEnable(false);
            }
        });
    }
    RoundedCorners roundedCorners = new RoundedCorners(16);
    RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
    /**
     * 生成视频封面
     */
    private void createVideoThumb() {
        Bitmap bitmap = null;
        //生成视频封面图
        MediaMetadataRetriever mmr = null;
        try {
            mmr = new MediaMetadataRetriever();
            if (Build.VERSION.SDK_INT >= 29) {
                mmr.setDataSource(mVideoPath);
            } else {
                mmr.setDataSource(mVideoPath);
            }
            bitmap = mmr.getFrameAtTime(-1);
            Logger.i("getVideoPath:" + mVideoPath + "----bitmap:" + bitmap.toString());
        } catch (Exception e) {
            bitmap = null;
            e.printStackTrace();
        } finally {
            if (mmr != null) {
                mmr.release();
            }
        }
        if (bitmap == null) {
            ToastUtil.show(com.manor.video.R.string.video_cover_img_failed);
            return;
        }
        final String coverImagePath = mVideoPath.replace(".mp4", ".jpg");
        File imageFile = new File(coverImagePath);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(imageFile);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            imageFile = null;
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (bitmap != null) {
            bitmap.recycle();
        }
        if (imageFile == null) {
            ToastUtil.show(com.manor.video.R.string.video_cover_img_failed);
            return;
        }
        final File finalImageFile = imageFile;
        //用鲁班压缩图片
        Luban.with(this)
                .load(finalImageFile)
                .setFocusAlpha(false)
                .ignoreBy(8)//8k以下不压缩
                .setTargetDir(CommonAppConfig.getPath())
                .setRenameListener(new OnRenameListener() {
                    @Override
                    public String rename(String filePath) {
                        filePath = filePath.substring(filePath.lastIndexOf("/") + 1);
                        return filePath.replace(".jpg", "_c.jpg");
                    }
                })
                .setCompressListener(new OnCompressListener() {
                    @Override
                    public void onStart() {

                    }

                    @Override
                    public void onSuccess(File file) {
                        if (finalImageFile.exists()) {
                       //     finalImageFile.delete();
                        }
                        UploadBean img = new UploadBean();
                        img.setOriginFile(file);
                        img.setType(UploadBean.IMG);
                        if (mUploadList != null) {
                            mUploadList.add(img);
                        }
                        ////
                        Glide.with(mContext).load(file)
                            .apply(options)
                                .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL))
                                .into(mIvVideoImg);

                        if (mLlUploadType != null) {
                            mLlUploadType.setVisibility(View.GONE);
                        }
                        if (mRlShowVideo != null) {
                            mRlShowVideo.setVisibility(View.VISIBLE);
                        }
                        setPublishEnable(true);
                    }

                    @Override
                    public void onError(Throwable e) {
                        KLog.e("tag",e+"");
                    }
                }).launch();
    }

    public String getVideoPath() {
        return mVideoPath;
    }

    /**
     * 删除视频
     */
    public void delVideo() {
        mPublishType = Constants.DYNAMIC_TYPE_TEXT;
        mVideoPath = "";
        if (mUploadList != null) {
            mUploadList.clear();
        }
        if (mLlUploadType != null) {
            mLlUploadType.setVisibility(View.VISIBLE);
        }
        if (mRlShowVideo != null) {
            mRlShowVideo.setVisibility(View.GONE);
        }
        setPublishEnable(false);
    }

    /**
     * 添加音频
     *
     * @param file
     * @param sumTime 总时长
     */
    public void addVoiceInfo(File file, int sumTime) {
        mPublishType = Constants.DYNAMIC_TYPE_VOICE;
        mVoiceFile = file;
        mVoiceSumTime = sumTime;
        UploadBean voice = new UploadBean();
        voice.setType(UploadBean.VOICE);
        voice.setOriginFile(file);
        mUploadList.clear();
        mUploadList.add(voice);
        if (mLlUploadType != null) {
            mLlUploadType.setVisibility(View.GONE);
        }
        if (mRlShowVoice != null) {
            mRlShowVoice.setVisibility(View.VISIBLE);
        }
        if (mVoicePlayView != null) {
            if (mVoiceMediaPlayerUtil == null) {
                mVoiceMediaPlayerUtil = new VoiceMediaPlayerUtil(mContext);
            }
            mVoicePlayView.setVoiceMediaPlayerUtil(mVoiceMediaPlayerUtil, new VoicePlayView.IAddListListener() {
                @Override
                public void deleteBank() {
                    delVoice();
                }
            });
            mVoicePlayView.setVoiceInfo(mVoiceSumTime, file.getAbsolutePath());
        }
        setPublishEnable(true);
    }


    //删除已录制音频
    private void delVoice() {
        DialogUitl.showSimpleDialog3(mContext, WordUtil.getString(R.string.is_del_voice), WordUtil.getString(R.string.delete), "", new DialogUitl.SimpleCallback() {
            @Override
            public void onConfirmClick(Dialog dialog, String content) {
                mPublishType = Constants.DYNAMIC_TYPE_TEXT;
                FilePathUtil.clearFilePath(mContext, mVoiceFile);
                mVoiceSumTime = 0;
                mVoiceFile = null;
                if (mUploadList != null) {
                    mUploadList.clear();
                }
                if (mLlUploadType != null) {
                    mLlUploadType.setVisibility(View.VISIBLE);
                }
                if (mRlShowVoice != null) {
                    mRlShowVoice.setVisibility(View.GONE);
                }
                if (mVoicePlayView != null) {
                    mVoicePlayView.resetView();
                }
                setPublishEnable(false);
            }
        });
    }

    private void publish() {
        mContent = mEtContent.getText().toString().trim();
        if (mPublishType == Constants.DYNAMIC_TYPE_TEXT && TextUtils.isEmpty(mContent)) {
            ToastUtil.show(WordUtil.getString(R.string.dynamic_null));
            return;
        }
        if (mLoading == null) {
            mLoading = DialogUitl.loadingDialog(mContext, WordUtil.getString(R.string.video_pub_ing));
        }
        mLoading.show();
        if (mPublishType == Constants.DYNAMIC_TYPE_TEXT) {
            doSubmit("", "", "", "");
        } else {
            uploadFile();
        }
    }

    private void uploadFile() {
        if (mUploadStrategy == null) {
            mUploadStrategy = new UploadQnImpl(mContext);
        }
        mUploadStrategy.upload(mUploadList, mPublishType == Constants.DYNAMIC_TYPE_IMG, new UploadCallback() {
            //        mUploadStrategy.upload(mUploadList,false, new UploadCallback() {
            @Override
            public void onFinish(List<UploadBean> list, boolean success) {
                if (success) {
                    //TODO 修改需调整 mUploadList.size()的判断
                    if (mPublishType == Constants.DYNAMIC_TYPE_IMG){
                        if (list != null  ) {
                            if(list.size()>8){
                                doUploadSuccess(list);
                            }else {
                                if(list.size() == (mUploadList.size()-1)){
                                    doUploadSuccess(list);
                                }else {
                                    doUploadError();
                                }
                            }

                        }else {
                            doUploadError();
                        }
                    }else {
                        if (list != null) {
                            doUploadSuccess(list);
                        }else {
                            doUploadError();
                        }
                    }

                } else {
                    doUploadError();
                }
            }
        });
    }

    private void doUploadError(){
        ToastUtil.show(WordUtil.getString(R.string.upload_fail));
        if (mLoading != null) {
            mLoading.dismiss();
        }
    }

    private void doUploadSuccess(List<UploadBean> list){
        if (mPublishType == Constants.DYNAMIC_TYPE_IMG) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0, size = list.size(); i < size; i++) {
                String fileName = list.get(i).getRemoteFileName();
                if (!TextUtils.isEmpty(fileName)) {
                    sb.append(fileName);
                    sb.append(";");
                }
            }
            String photos = sb.toString();
            if (photos.length() > 1) {
                photos = photos.substring(0, photos.length() - 1);
            }
            doSubmit(photos, "", "", "");
            L.e("上传图片完成---------> " + photos);
        } else if (mPublishType == Constants.DYNAMIC_TYPE_VIDEO) {
            String videoPath = "";
            String imgPath = "";

            if (StringUtil.isNotEmpty(list.get(0).getRemoteFileName())) {
                videoPath = list.get(0).getRemoteFileName();
            }

            if (list.size() >= 2) {
                if (StringUtil.isNotEmpty(list.get(1).getRemoteFileName())) {
                    imgPath = list.get(1).getRemoteFileName();
                }
            }

            L.e("上传视频完成---------> " + videoPath);
            L.e("上传视频完成------img---> " + imgPath);
            doSubmit("", imgPath, videoPath, "");
        } else if (mPublishType == Constants.DYNAMIC_TYPE_VOICE) {
            String voicePath = list.get(0).getRemoteFileName();
            L.e("上传语音完成---------> " + voicePath);
            doSubmit("", "", "", voicePath);
        }
    }

    private void doSubmit(String photos, String videoThumb, String videoPath, String voicePath) {
        DynamicHttpUtil.uploadDynamic(mCbLocation.isChecked(), mContent, photos, videoThumb, videoPath, voicePath, mVoiceSumTime, mPublishType, new HttpCallback() {
            @Override
            public void onSuccess(int code, String msg, String info) {
                if (code == 0) {
                    EventBus.getDefault().post(new SubmitDynamiFinish(1));
                    finish();
                } else {
                    setPublishEnable(true);
                }
                ToastUtil.show(msg);
            }

            @Override
            public void onError() {
                super.onError();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                if (mLoading != null) {
                    mLoading.dismiss();
                }
            }
        });
    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (ClickUtil.canClick()) {
            if (i == R.id.btn_publish) {
                publish();
            } else if (i == R.id.btn_img || i == R.id.btn_addimg) {
                chooseImage();
            } else if (i == R.id.btn_video) {
                chooseVideo();
            } else if (i == R.id.btn_voice) {
                recordVoice();
            } else if (i == R.id.last_img) {
//                lookAllImgs();
            } else if (i == R.id.btn_del_img) {
//                delAllImgs();
            } else if (i == R.id.rl_video_show) {
                lookVideo();
            } else if (i == R.id.btn_voice_del) {
                delVoice();
            } else if (i == R.id.btn_del_video) {
                DialogUitl.showSimpleDialog3(mContext, WordUtil.getString(R.string.is_del_video), WordUtil.getString(R.string.delete), "", new DialogUitl.SimpleCallback() {
                    @Override
                    public void onConfirmClick(Dialog dialog, String content) {
                        delVideo();
                    }
                });
            }
        }
    }

    //    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void onVideoGenerateSucEvent(VideoGenerateSucEvent e) {
//        mPublishType = Constants.DYNAMIC_TYPE_VIDEO;
//        mVideoPath = e.getGenerateVideoPath();
//        UploadBean video = new UploadBean();
//        video.setOriginFile(new File(mVideoPath));
//        video.setType(UploadBean.VIDEO);
//        if (mUploadList != null) {
//            mUploadList.clear();
//        }
//        mUploadList.add(video);
//        createVideoThumb();
//    }
    private void setImgSize() {
        if (mUploadList.size() < 1||mUploadList.size()==9) {
            mBtnAddImg.setVisibility(View.GONE);
            for (int i = 0; i < mUploadList.size(); i++) {
                if (mUploadList.get(i).getOriginFile().equals("")) {
                    mUploadList.remove(i);

                }
            }


        } else {
            if(mUploadList.size() == 1&&null==mUploadList.get(0).getOriginFile()){
                mUploadList.clear();
                setAdapter();
                return;
            }
            //      mBtnAddImg.setVisibility(View.VISIBLE);
            boolean is_type = true;
            for (int i = 0; i < mUploadList.size(); i++) {
                if (null == mUploadList.get(i).getOriginFile()) {
                    is_type = false;
                }
            }
            if (is_type) {
                UploadBean uploadBean1 = new UploadBean(null, "", "", false, 1, false);
                mUploadList.add(uploadBean1);
            }

        }
        setAdapter();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onChooseLocalVideo(ChooseVideoEvent e) {
        mVideoPath = e.getVideoPath();
        if (!TextUtils.isEmpty(mVideoPath)) {
            mPublishType = Constants.DYNAMIC_TYPE_VIDEO;
            UploadBean video = new UploadBean();
            video.setOriginFile(new File(mVideoPath));
            video.setType(UploadBean.VIDEO);
            if (mUploadList != null) {
                mUploadList.clear();
            }
            mUploadList.add(video);

         createVideoThumb();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRecordVideo(VideoRecordEvent e) {
        mPublishType = Constants.DYNAMIC_TYPE_VIDEO;
        mVideoPath = e.getVideoPath();
        UploadBean video = new UploadBean();
        video.setOriginFile(new File(mVideoPath));
        video.setType(UploadBean.VIDEO);
        if (mUploadList != null) {
            mUploadList.clear();
        }
        mUploadList.add(video);
        createVideoThumb();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onSelectImg(final ImgEvent imgEvent) {
        if (imgEvent.getImgBeanList() == null) {
            return;
        }
        if (imgEvent.getImgBeanList().size() == 0) {
            return;
        }
        if (mLlUploadType != null) {
            mLlUploadType.setVisibility(View.GONE);
            rvPublishImg.setVisibility(View.VISIBLE);
        }
        if (mLlShowImg != null) {
            mLlShowImg.setVisibility(View.VISIBLE);
        }
        mBtnPublish.setEnabled(true);
        mBtnPublish.setBackgroundResource(R.drawable.bg_dynamic_publishings);
        mPublishType = Constants.DYNAMIC_TYPE_IMG;

        rvPublishImg.setVisibility(View.VISIBLE);

        //TODO 修改九宫格照片
        mUploadList.addAll(mUploadList.size(), imgEvent.getImgBeanList());

        setImgSize();

    }

    private void setAdapter() {
        FullyGridLayoutManager manager = new FullyGridLayoutManager(this,
                3, GridLayoutManager.VERTICAL, false);
        rvPublishImg.setLayoutManager(manager);
//        rvPublishImg.addItemDecoration(new GridSpacingItemDecoration(3,
//                ScreenUtils.dip2px(this, 5), false));
        publishImgAdapter = new PublishImgAdapter(R.layout.activity_dynamic_publish_img_item, mUploadList);
        rvPublishImg.setAdapter(publishImgAdapter);
        publishImgAdapter.addChildClickViewIds(R.id.btn_del_img);
        publishImgAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NotNull BaseQuickAdapter adapter, @NotNull View view, int position) {
           //     ToastUtil.show("删除这张图片");
                if (position > mUploadList.size()) {
                    return;
                } else {

                }
                mUploadList.remove(position);
                selectImgList.remove(position);
//                imgEvent.getImgBeanList().remove(position);

                setImgSize();

                if (mUploadList.size() < 1) {
                    mBtnAddImg.setVisibility(View.GONE);
                    rvPublishImg.setVisibility(View.GONE);
                    mLlUploadType.setVisibility(View.VISIBLE);
                } else {
                    mLlUploadType.setVisibility(View.GONE);
                    //    mBtnAddImg.setVisibility(View.VISIBLE);
                }


                publishImgAdapter.notifyDataSetChanged();
            }
        });

        publishImgAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NotNull BaseQuickAdapter adapter, @NotNull View view, int position) {
                if (null != mUploadList.get(position).getOriginFile()) {
                    lookAllImgs(position);
                } else {
                    chooseImage();
                }

            }
        });
    }

    @Override
    public void onBackPressed() {
        mContent = mEtContent.getText().toString().trim();
        if (mUploadList != null && mUploadList.size() > 0 || mContent != null && mContent.length() > 0) {
            DialogUitl.showSimpleDialog2(mContext, WordUtil.getString(R.string.is_giveup_edit), new DialogUitl.SimpleCallback() {
                @Override
                public void onConfirmClick(Dialog dialog, String content) {
                    finish();
                }
            });
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mVoicePlayView != null) {
            mVoicePlayView.release();
        }
        EventBus.getDefault().unregister(this);
        DynamicHttpUtil.cancel(DynamicHttpConsts.DYNAMIC_PUBLISH);
    }


}
